package top.ldkxingzhe.ldksettings

import android.annotation.SuppressLint
import android.app.LdkManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageParser
import android.os.Bundle
import android.os.ServiceManager
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import kotlinx.android.synthetic.main.activity_detail.*
import top.ldkxingzhe.ldksettings.actions.PMHelper
import top.ldkxingzhe.ldksettings.adapters.ComponentAdapter
import top.ldkxingzhe.ldksettings.entities.ComponentEntity
import java.util.*
import kotlin.collections.HashMap

/**
 * Created by liangdengke on 2018/10/22.
 */
class DetailActivity: AppCompatActivity() {
    private val TAG = "DetailActivity"

    private lateinit var recyclerView: RecyclerView
    private lateinit var progressBar: ProgressBar
    private lateinit var appPackageName: String
    private lateinit var ldkManager: LdkManager

    @SuppressLint("WrongConstant")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)
        recyclerView = recyclerview
        progressBar = pb_progress
        appPackageName = intent.getStringExtra("app")

        loadReceiverInfos()
        ldkManager = getSystemService("ldkservice") as LdkManager

        btn_add_whitelist.setOnClickListener {
            Log.d(TAG, "addWhiteList")
            ldkManager.addPackageWhiteList(appPackageName)
        }

        btn_action.setOnClickListener {
            val result = ldkManager.isPackageInWhiteList(appPackageName)
            Log.d(TAG, "isPackageInWhiteList: $result")

            val intent = Intent("top.ldkxingzhe.top.action.test")
            intent.addFlags(0x01000000)
            sendBroadcast(intent)
        }
    }

    private fun loadReceiverInfos(){
        Thread{
            Log.d(TAG, "loadReceiverInfos: $appPackageName")
            val applicationInfo = packageManager.getApplicationInfo(appPackageName, 0)
            val pmHelper = PMHelper(this)
            val pkg = pmHelper.parsePackage(applicationInfo)
            val components = pkg.receivers?.map {
                val actions = it.intents?.map {innerIntent ->
                    val builder = StringBuilder()
                    for (i in 0 until innerIntent.countActions()){
                        builder.append(innerIntent.getAction(i)).append(", ")
                    }
                    builder.toString()
                }?.joinToString(separator = "\n")?: ""
                ComponentEntity(it.className, actions)
            }?: Collections.emptyList()
            runOnUiThread {
                initRecyclerViewAdapter(pkg, components)
            }
        }.start()
    }

    private fun initRecyclerViewAdapter(pkg: PackageParser.Package, list: List<ComponentEntity>){
        progressBar.visibility = View.GONE
        val adapter = ComponentAdapter(list)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        title = pkg.packageName
    }
}